package com.suozhang.framework.utils;

import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;

/**
 * @ProjectName: ZhiYuan
 * @Package: com.suozhang.framework.utils
 * @ClassName: TextUtil
 * @Description: 改变textView某部分字体颜色
 * @Author:
 * @CreateDate: 2019/3/11 15:58
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/3/11 15:58
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class TextUtil {

    /**
     * 改变TextView部分字体的颜色
     *
     * @param originalValue
     * @param indexValue
     * @return
     */

    public static SpannableStringBuilder setTextViewColor(String originalValue, String indexValue, int color) {
        SpannableStringBuilder builder = new SpannableStringBuilder(originalValue);
        if (TextUtils.isEmpty(originalValue)) {
            return builder;
        }
        for (int j = 0; j < originalValue.length(); j++) {
            if (j + indexValue.length() <= originalValue.length()) {
                String name = (String) originalValue.subSequence(j, j + indexValue.length());
                if (name.equals(indexValue)) {
                    ForegroundColorSpan redSpan = new ForegroundColorSpan(color);
                    builder.setSpan(redSpan, j, j + indexValue.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
            }
        }
        return builder;
    }

    /**
     * 改变TextView部分字体的颜色和大小
     *
     * @param originalValue
     * @param indexValue
     * @return
     */
    public static SpannableStringBuilder setTextViewColorSize(String originalValue, String indexValue, int color, int size) {
        SpannableStringBuilder builder = new SpannableStringBuilder(originalValue);
        if (TextUtils.isEmpty(originalValue)) {
            return builder;
        }
        for (int j = 0; j < originalValue.length(); j++) {
            if (j + indexValue.length() <= originalValue.length()) {
                String name = (String) originalValue.subSequence(j, j + indexValue.length());
                if (name.equals(indexValue)) {
                    ForegroundColorSpan redSpan = new ForegroundColorSpan(color);
                    builder.setSpan(new AbsoluteSizeSpan(size), j, j + indexValue.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    builder.setSpan(redSpan, j, j + indexValue.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
            }
        }
        return builder;
    }
}
